#include "Vegetation.h"

Vegetation::Vegetation(glm::vec3 _Pos, float _r, std::string _Name,
		std::string _modelFName, ModelPool *_mPool, PathFinder *_pathfinder) :
		staticEntity(_Pos, _r, _Name, _modelFName, _mPool, _pathfinder)
{
	randTimeStart = rand() % 1000;

	type = OBJ_VEGETATION;
	parentType = OBJ_STATIC;

	// override default shader id

	shaderID = Shader::sPool->getProgramID("vegetation");
	shadowShaderID = Shader::sPool->getProgramID("vegetation_sh");
}

Vegetation::~Vegetation(void)
{
}

void Vegetation::draw(int shadertype)
{
	// exit when unit is sleeping
	if (sleeping)
		return;

	float time = (float) (SDL_GetTicks() + randTimeStart) / 1000.0;

	glm::vec3 pfrust = Pos;
	pfrust.y = pfrust.y + 1.5;

	// using normal shader for object
	if ((shadertype == SHADER_NORMAL) || (shadertype == SHADER_REFLECTION))
	{

		// increase counter (for counting visible objects)
		World::counter += 1;

		// get matrix
		glm::mat4 ModelMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(age / 100.0));
		glm::mat4 ViewMatrix = glm::translate(World::world->getView(),
				glm::vec3(Pos.x, Pos.y, Pos.z));
		ViewMatrix = glm::rotate(ViewMatrix, r, glm::vec3(0.0f, 1.0f, 0.0f));
		glm::mat4 MVP = World::world->getProjection() * ViewMatrix * ModelMatrix;

		// get light information
		glm::vec3 LightPos = World::world->getLightPos(); // position of light
		glm::vec3 LightDif = World::world->getLightDif(); // diffuse color of light
		glm::vec3 LightAmb = World::world->getLightAmb(); // ambient color of light
		glm::vec3 LightSpec = World::world->getLightSpe(); // specular color of light

		glUseProgram(shaderID);

		// send matrix to shader
		glUniformMatrix4fv(glGetUniformLocation(shaderID, "MVP"), 1, GL_FALSE,
				&MVP[0][0]);
		glUniformMatrix4fv(glGetUniformLocation(shaderID, "M"), 1, GL_FALSE,
				&ModelMatrix[0][0]);
		glUniformMatrix4fv(glGetUniformLocation(shaderID, "V"), 1, GL_FALSE,
				&ViewMatrix[0][0]);
		glUniform3f(glGetUniformLocation(shaderID, "LightPosition_worldspace"),
				LightPos.x, LightPos.y, LightPos.z);
		glUniform3f(glGetUniformLocation(shaderID, "lightAmb"), LightAmb.x,
				LightAmb.y, LightAmb.z);
		glUniform3f(glGetUniformLocation(shaderID, "lightDif"), LightDif.x,
				LightDif.y, LightDif.z);
		glUniform3f(glGetUniformLocation(shaderID, "lightSpec"), LightSpec.x,
				LightSpec.y, LightSpec.z);

		glUniform1i(glGetUniformLocation(shaderID, "tex0"), 0);
		glUniform1f(glGetUniformLocation(shaderID, "time"), time);
		glUniform1f(glGetUniformLocation(shaderID, "ground"), Pos.y);

		// draw model
		mPool->drawModelByID(modelID[0]);
		glUseProgram(0);

		drawMarker(1.0); // draw marker
	}
	// using shader for shadows
	else if (shadertype == SHADER_SHADOW)
	{
		// get shadow matrix
		glm::mat4 ModelMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(age / 100.0));
		glm::mat4 ViewMatrix = glm::translate(World::world->getShadowView(),
				glm::vec3(Pos.x, Pos.y, Pos.z));
		ViewMatrix = glm::rotate(ViewMatrix, r, glm::vec3(0.0f, 1.0f, 0.0f));
		glm::mat4 MVP = World::world->getShadowProjection() * ViewMatrix
				* ModelMatrix;

		glUseProgram(shadowShaderID);

		// send matrix to shader
		glUniformMatrix4fv(glGetUniformLocation(shadowShaderID, "depthMVP"), 1,
				GL_FALSE, &MVP[0][0]);

		glUniform1i(glGetUniformLocation(shadowShaderID, "tex0"), 0);
		glUniform1f(glGetUniformLocation(shadowShaderID, "time"), time);
		glUniform1f(glGetUniformLocation(shadowShaderID, "ground"), Pos.y);

		mPool->drawModelByID(modelID[0]);

		glUseProgram(0);

	}

}
